package com.example.yichuangyun.mvvm.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemInfoLabelBinding;
import com.example.yichuangyun.model.company.CompanyLabelModel;

import java.util.ArrayList;
import java.util.List;

public class CompanyLabelAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private List<CompanyLabelModel> mData;
    private OnLabelItemClickListener onLabelItemClickListener;
    private int itemCount;

    public CompanyLabelAdapter(Context context, List<CompanyLabelModel> mData) {
        this.context = context;
        this.mData = mData;
        itemCount = mData.size();
    }

    public void onRefresh(List<CompanyLabelModel> mList) {
        if (mList != null) {
            if (mData == null) {
                mData = new ArrayList<>();
            }
            this.mData.clear();
            this.mData.addAll(mList);
            itemCount = Math.min(mData.size(), 9);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //在这里根据不同的viewType进行引入不同的布局
        ItemInfoLabelBinding itemLabelBinding = ItemInfoLabelBinding.inflate(LayoutInflater.from(context), parent, false);
        return new MyViewHolder(itemLabelBinding);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            CompanyLabelModel model = mData.get(position);
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            myViewHolder.binding.tvTitle.setText(model.getTitle());
            if (model.getTitle().equals(context.getString(R.string.gsxx))) {
                myViewHolder.binding.ivLogo.setImageResource(model.getImgSelResource());
                myViewHolder.binding.tvCount.setVisibility(View.GONE);
            } else {
                myViewHolder.binding.ivLogo.setImageResource(model.getCount() > 0 ? model.getImgSelResource() : model.getImgResource());
                myViewHolder.binding.tvCount.setText(String.valueOf(model.getCount()));
                myViewHolder.binding.tvCount.setVisibility(model.getCount() > 0 ? View.VISIBLE : View.GONE);
                myViewHolder.binding.tvTitle.setTextColor(model.getCount() > 0 ? context.getColor(R.color.color_131415) : context.getColor(R.color.color_BBC2CB));
                myViewHolder.itemView.setEnabled(model.getCount() > 0);
            }
            myViewHolder.itemView.setOnClickListener(view -> {
                if (null != onLabelItemClickListener) {
                    onLabelItemClickListener.onLabelItemClickListener(holder.getAbsoluteAdapterPosition(), model);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return itemCount;
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemInfoLabelBinding binding;

        public MyViewHolder(ItemInfoLabelBinding itemInfoLabelBinding) {
            super(itemInfoLabelBinding.getRoot());
            binding = itemInfoLabelBinding;
        }
    }

    public void setOnLabelItemClickListener(OnLabelItemClickListener onLabelItemClickListener) {
        this.onLabelItemClickListener = onLabelItemClickListener;
    }

    public interface OnLabelItemClickListener {
        void onLabelItemClickListener(int position, CompanyLabelModel model);
    }
}
